﻿// DensitySlicing.cpp: 实现文件
//

#include "pch.h"
#include "DensitySlicing.h"
#include "afxdialogex.h"
#include"resource.h"
#include"DensitySlicingAttributeDialog.h"

// DensitySlicing 对话框

     /*全局变量区域*/

CString filePath;//图像文件路径
CImage readImage;

int qishix = 0, qishiy = 0, zhongdianx = 300, zhongdiany = 300;
extern CImage MidImage;
IMPLEMENT_DYNAMIC(DensitySlicing, CDialogEx)

DensitySlicing::DensitySlicing(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_DensitySlicing, pParent)
{

}

DensitySlicing::~DensitySlicing()
{
}

void DensitySlicing::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(DensitySlicing, CDialogEx)
	ON_COMMAND(ID_DENSITYDLG, &DensitySlicing::OnOpenAttributeDialog)
	ON_COMMAND(ID_32778, &DensitySlicing::OnOpenFile)
	ON_WM_PAINT()
//	ON_WM_NCMOUSEMOVE()
	ON_WM_MOUSEWHEEL()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_CANOPYCLU, &DensitySlicing::OnCanopyclu)
	ON_COMMAND(ID_DENSITYDLG, &DensitySlicing::OnDensitydlg)
	ON_COMMAND(ID_ASD, &DensitySlicing::OnAsd)
	ON_COMMAND(ID_JIHEJIAOZHENG, &DensitySlicing::OnJihejiaozheng)
	ON_COMMAND(ID_32779, &DensitySlicing::On32779)
END_MESSAGE_MAP()


// DensitySlicing 消息处理程序


void DensitySlicing::OnOpenAttributeDialog()
{
	// TODO: 在此添加命令处理程序代码

	DensitySlicingAttributeDialog *dlg = new DensitySlicingAttributeDialog;
	dlg->Create(IDD_DIALOG_DSAttribute, this);
	dlg->ShowWindow(SW_SHOW);
}


void DensitySlicing::OnOpenFile()
{
	// TODO: 在此添加命令处理程序代码

	BOOL isOpen = TRUE;		//是否打开(否则为保存)
	CString defaultDir = L"D:";	//默认打开的文件路径
	CString fileName = L"";			//默认打开的文件名
	CString filter = L"文件 (*.bmp; *.jpeg; *.png)||";	//文件过虑的类型

	CFileDialog openFileDlg(isOpen, defaultDir, fileName, OFN_HIDEREADONLY | OFN_READONLY, filter, NULL);
	INT_PTR result = openFileDlg.DoModal();
	
	if (result == IDOK) {
		filePath = openFileDlg.GetPathName();
	}
	//AfxMessageBox(_T("文件打开成功！！！"));
	//获取设备句柄
	
	if (filePath == _T(""))
	{
		return;
	}

	//得到当前设备区域
	readImage.Load(filePath);//LC81190392015286LGN007-5-4.jpg //R-C.jpg//mmexport1626586881792.bmp
	//显示未处理前图像

	int picWidth = readImage.GetWidth();
	int picHeight = readImage.GetHeight();
	CRect picRect;
	GetClientRect(&picRect);
	int width = picRect.right - picRect.left;
	int height = picRect.bottom - picRect.top;
	if (width / (float)picWidth > height / (float)picHeight)
	{
		qishiy = 0; zhongdiany = height;
		zhongdianx = int(picWidth / (float)picHeight * height);
		qishix = int((float)width / 2 - (float)zhongdianx / 2);
	}
	else if (width / (float)picWidth <= height / (float)picHeight)
	{
		qishix = 0; zhongdianx = width;
		zhongdiany = int(picHeight / (float)picWidth * width);
		qishiy = int((float)height / 2 - (float)zhongdiany / 2);
	}

	Invalidate(FALSE);
}

void DensitySlicing::OnPaint()
{
	pDC = GetDC();

	pHDC = pDC->GetSafeHdc();
	CPaintDC dc(this);

	CRect picRect;
	GetClientRect(&picRect);
	int width = picRect.right - picRect.left;
	int height = picRect.bottom - picRect.top;

	if (readImage && !MidImage)
	{
		CPen temppen;
		temppen.CreatePen(PS_NULL, 0, RGB(255, 255, 255));
		CPen* oldpen = dc.SelectObject(&temppen);
		dc.SelectObject(GetSysColorBrush(COLOR_WINDOW));
		dc.Rectangle(0, 0, qishix + dx, qishiy + dy + zhongdiany);
		dc.Rectangle(qishix + dx, 0, width, qishiy + dy);
		dc.Rectangle(0, qishiy + dy + zhongdiany, qishix + dx + zhongdianx, height);
		dc.Rectangle(qishix + dx + zhongdianx, qishiy + dy, width, height);
		::SetStretchBltMode(*pDC, HALFTONE);
		::SetBrushOrgEx(*pDC, 0, 0, NULL);
		readImage.Draw(pHDC, qishix + dx, qishiy + dy, zhongdianx, zhongdiany);
		dc.SelectObject(oldpen);
		temppen.DeleteObject();
	}
	else if(!MidImage)
	{
		dc.SelectObject(GetSysColorBrush(COLOR_WINDOW));
		dc.Rectangle(0, 0, width, height);
	}

	if (MidImage)
	{
		CPen temppen;
		temppen.CreatePen(PS_NULL, 0, RGB(255, 255, 255));
		CPen* oldpen = dc.SelectObject(&temppen);
		dc.SelectObject(GetSysColorBrush(COLOR_WINDOW));
		dc.Rectangle(0, 0, qishix + dx, qishiy + dy + zhongdiany);
		dc.Rectangle(qishix + dx, 0, width, qishiy + dy);
		dc.Rectangle(0, qishiy + dy + zhongdiany, qishix + dx + zhongdianx, height);
		dc.Rectangle(qishix + dx + zhongdianx, qishiy + dy, width, height);
		::SetStretchBltMode(*pDC, HALFTONE);
		::SetBrushOrgEx(*pDC, 0, 0, NULL);
		MidImage.Draw(pHDC, qishix + dx, qishiy + dy, zhongdianx, zhongdiany);
		dc.SelectObject(oldpen);
		temppen.DeleteObject();
	}	
}

BOOL DensitySlicing::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (readImage)
	{
		if (zDelta > 0)
		{
			float x = MovePoint.x - qishix;
			float y = MovePoint.y - qishiy;
			x = x * 1.05;
			y = y * 1.05;
			qishix = int(MovePoint.x - x);
			qishiy = int(MovePoint.y - y);
			zhongdianx = zhongdianx * 1.05;
			zhongdiany = zhongdiany * 1.05;
			Invalidate(FALSE);
		}
		if (zDelta < 0)
		{
			float x = MovePoint.x - qishix;
			float y = MovePoint.y - qishiy;
			x = x * 0.95;
			y = y * 0.95;
			qishix = int(MovePoint.x - x);
			qishiy = int(MovePoint.y - y);
			zhongdianx = zhongdianx * 0.95;
			zhongdiany = zhongdiany * 0.95;
			Invalidate(FALSE);
		}
	}

	return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
}


void DensitySlicing::OnMouseMove(UINT nFlags, CPoint point)
{
	MovePoint = point;
	if (bButtonDown == TRUE && Ismove == TRUE && readImage)
	{
        dx = point.x - TempPoint.x;
		dy = point.y - TempPoint.y;
		Invalidate(FALSE);
	}
	int a = 0;
	CDialogEx::OnMouseMove(nFlags, point);
}


void DensitySlicing::OnLButtonDown(UINT nFlags, CPoint point)
{
	bButtonDown = TRUE;
	TempPoint.x = point.x;
	TempPoint.y = point.y;

	CDialogEx::OnLButtonDown(nFlags, point);
}


void DensitySlicing::OnLButtonUp(UINT nFlags, CPoint point)
{
	bButtonDown = FALSE;
	if (Ismove == TRUE && readImage)
	{
		dx = point.x - TempPoint.x;
		dy = point.y - TempPoint.y;
		qishix += dx;
		qishiy += dy;
		Invalidate(FALSE);
		dx = dy = 0;
		readImage.Draw(pHDC, qishix, qishiy, zhongdianx, zhongdiany);
	}
	
	CDialogEx::OnLButtonUp(nFlags, point);
}


BOOL DensitySlicing::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类

	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam) {
		case VK_SPACE:
			Ismove = TRUE;
			break;
		}
	}
	if (pMsg->message == WM_KEYUP)
	{
		switch (pMsg->wParam) {
		case VK_SPACE:
			Ismove = FALSE;
			break;
		}
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}


void DensitySlicing::OnCanopyclu()
{
	CanopyCluster = new CanopyClustering(filePath);

	CanopyCluster->m_DensitySlicing = this;

	// TODO: 在此添加命令处理程序代码
}


void DensitySlicing::OnDensitydlg()
{
	DensitySlicingAttributeDialog *dlg = new DensitySlicingAttributeDialog;
	dlg->Create(IDD_DIALOG_DSAttribute, this);
	dlg->ShowWindow(SW_SHOW);
	// TODO: 在此添加命令处理程序代码
}


void DensitySlicing::OnAsd()
{
	CSpectrumDlg * SpectrumDlg;
	SpectrumDlg = new CSpectrumDlg;
	SpectrumDlg->Create(IDD_SPECTRUM_DIALOG, this);
	SpectrumDlg->ShowWindow(SW_SHOW);
	// TODO: 在此添加命令处理程序代码
}


void DensitySlicing::OnJihejiaozheng()
{
	CBandListDlg * BandListDlg;
	BandListDlg = new CBandListDlg;
	BandListDlg->Create(IDD_DIALOG_BANDLIST, this);
	BandListDlg->ShowWindow(SW_SHOW);
	// TODO: 在此添加命令处理程序代码
}


BOOL DensitySlicing::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	pDC = GetDC();
	

	pHDC = pDC->GetSafeHdc();
	Ismove = FALSE;
	// TODO:  在此添加额外的初始化
	bButtonDown = FALSE;
	TempPoint.x = TempPoint.y = 0;
	dx = dy = 0;
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void DensitySlicing::On32779()
{
	if (!readImage && !MidImage)
	{
		AfxMessageBox(_T("没有图像可以导出"));
		return;
	}
	CString filename;
	LPCTSTR szFilter = _T("Any Known Formats (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg|All Files (*.*)|*.*|");		//提示保存文件的类型
	CFileDialog output_img(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (output_img.DoModal() == IDOK)
	{
		filename = output_img.GetPathName();		//保存文件的路径到filename变量中
	}
	if (MidImage)
	{
		HRESULT hRet = MidImage.Save(filename);
		if (FAILED(hRet))
		{
			_com_error e(hRet);
			CString str;
			str.Format(_T("图像保存失败: %s"), e.ErrorMessage());
			AfxMessageBox(str);
			throw hRet;    // 抛出错误
		}
		AfxMessageBox(_T("导出成功"));
	}
	else if(readImage)
	{
		HRESULT hRet = readImage.Save(filename);
		if (FAILED(hRet))
		{
			_com_error e(hRet);
			CString str;
			str.Format(_T("图像保存失败: %s"), e.ErrorMessage());
			AfxMessageBox(str);
			throw hRet;    // 抛出错误
		}
		AfxMessageBox(_T("导出成功"));
	}
}
